package com.zondy.mapgis.psmap.manager.controller;

import cn.hutool.core.lang.Dict;
import com.zondy.mapgis.common.controllerprefix.annotation.ManagerRestController;
import com.zondy.mapgis.common.core.utils.JsonUtils;
import com.zondy.mapgis.common.core.web.domain.AjaxResult;
import com.zondy.mapgis.domain.psmap.AppTheme;
import com.zondy.mapgis.domain.psmap.AppWidget;
import com.zondy.mapgis.psmap.manager.service.IAppStoreService;
import com.zondy.mapgis.psmap.manager.service.IAppThemeService;
import com.zondy.mapgis.psmap.manager.service.IAppWidgetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.LinkedHashMap;
import java.util.Map;

@Tag(
   name = "参数配置管理",
   description = "参数配置控制器"
)
@ManagerRestController({"/system/webConfig"})
public class SysAppWebConfigController {
   private final IAppWidgetService appWidgetService;
   private final IAppThemeService appThemeService;
   private final IAppStoreService appStoreService;

   @Operation(
      summary = "获取系统配置信息"
   )
   @GetMapping({"/app/resource"})
   public AjaxResult getAppResource() {
      Map<String, Object> appResource = new LinkedHashMap<>();
      appResource.put("widgetCount", this.appWidgetService.selectAppWidgetList(new AppWidget()).size());
      appResource.put("themeCount", this.appThemeService.selectAppThemeList(new AppTheme()).size());
      return AjaxResult.success(appResource);
   }

   @Operation(
      summary = "获取应用基本配置信息"
   )
   @GetMapping({"/app/base"})
   public AjaxResult getBaseConfig() {
      Map<String, Object> systemConfig = new LinkedHashMap<>();
      String appConfig = this.appStoreService.selectAppStoreByAppId(1L).getAppConfig();
      Dict dictAppConfig = JsonUtils.parseMap(appConfig);
      systemConfig.put("logo", dictAppConfig.get("logo"));
      systemConfig.put("title", dictAppConfig.get("title"));
      systemConfig.put("subtitle", dictAppConfig.get("subtitle"));
      systemConfig.put("copyright", dictAppConfig.get("copyright"));
      return AjaxResult.success(systemConfig);
   }

   @Autowired
   public SysAppWebConfigController(IAppWidgetService appWidgetService, IAppThemeService appThemeService, IAppStoreService appStoreService) {
      this.appWidgetService = appWidgetService;
      this.appThemeService = appThemeService;
      this.appStoreService = appStoreService;
   }
}
